package cn.edu.sjtu.software.sjtucontacts.activity;

import cn.edu.sjtu.software.sjtucontacts.R;
import cn.edu.sjtu.software.sjtucontacts.model.Preference;
import cn.edu.sjtu.software.sjtucontacts.util.ColorPickerDialog;
import cn.edu.sjtu.software.sjtucontacts.util.ListUtil;
import cn.edu.sjtu.software.sjtucontacts.util.ColorPickerDialog.OnColorChangedListener;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class SetPreferenceActivity extends Activity{
	
	private TextView fontSizeResult,fontColorResult;
	private Spinner fontSizeSpinner;
	private Button fontColorButton,confirmButton,cancelButton;
	private String fontSizes[] = {"12","14","16","18","20"};
	private int fontSize;
	private int fontColor;
	private int encryAlgorithm,theme;
	private RadioGroup radioGroup,themeGroup;
	private static SetPreferenceActivity instance;
	
	@Override
    public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
        this.setContentView(R.layout.set_preference);
        SetPreferenceActivity.instance = this;
        fontSizeResult = (TextView)this.findViewById(R.id.set_preference_font_size_result);
        fontSizeSpinner = (Spinner)this.findViewById(R.id.set_preference_font_size_spinner);
        fontColorButton = (Button)this.findViewById(R.id.set_preference_font_color_button);
        confirmButton = (Button)this.findViewById(R.id.set_preference_confirmbutton);
        cancelButton = (Button)this.findViewById(R.id.set_preference_cancelButton);
        
        fontSizeResult.setTextSize(Preference.getInstance().getFontSizeOfName());
        fontSizeResult.setTextColor(Preference.getInstance().getFontColorOfName());
        fontColor = Preference.getInstance().getFontColorOfName();
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item);
        for(int i=0;i<fontSizes.length;i++){
        	adapter.add(fontSizes[i]);
        }
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        fontSizeSpinner.setAdapter(adapter);
        fontSizeSpinner.setSelection(0);
        for(int i=0;i<fontSizes.length;i++){
        	if(Preference.getInstance().getFontSizeOfName()==Integer.parseInt(fontSizes[i])){
        		fontSizeSpinner.setSelection(i);
        		fontSize = Preference.getInstance().getFontSizeOfName();
        		break;
        	}
        }
        
        fontColorButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				ColorPickerDialog colorPicker = new ColorPickerDialog(SetPreferenceActivity.getInstance(), new OnColorChangedListener(){

					@Override
					public void colorChanged(int color) {
						fontColor = color;
					}
					
				}, new android.content.DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						setFontColor(fontColor);
						
					}
					
				},Preference.getInstance().getFontColorOfName());
				colorPicker.show();
			}
        	
        });
        
        confirmButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				Preference.getInstance().setFontSizeOfName(fontSize);
				Preference.getInstance().setFontColorOfName(fontColor);
				Preference.getInstance().setEncryptAlgorithmName(encryAlgorithm);
				Preference.getInstance().setTheme(theme);
				Preference.getInstance().savePreference();
				new Thread() {
					public void run() {
						PullService.writeContacts(ListUtil.getInstance().fetchContactList());
						PullService.writeGroups(ListUtil.getInstance().getGroupList());
					}
				}.start();
				Intent result = new Intent("RETURN_PREFERENCE", null);
				setResult(RESULT_OK, result);
				finish();
			}
        	
        });
        
        cancelButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				setResult(RESULT_CANCELED, null);
				finish();
			}
        	
        });
        
        fontSizeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				setFontSize(position);
				System.out.println("set font size num:"+position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
        
        encryAlgorithm = Preference.getInstance().getEncryptAlgorithmName();
        radioGroup = (RadioGroup) findViewById(R.id.set_preference_radioGroup);
        radioGroup.check(R.id.set_preference_radioButtonAlgo0+encryAlgorithm);
        radioGroup.setOnCheckedChangeListener(
        		new RadioGroup.OnCheckedChangeListener() {
        		public void onCheckedChanged(
        		RadioGroup group,
        		int checkedId) {
						setEncryAlgorithm(checkedId-R.id.set_preference_radioButtonAlgo0);
						System.out.println("select algorithm: "+checkedId);
        		}
        		});
        
        theme = Preference.getInstance().getTheme();
        themeGroup = (RadioGroup) findViewById(R.id.set_preference_themeGroup);
        themeGroup.check(R.id.set_preference_radioButtonTheme0+theme);
        themeGroup.setOnCheckedChangeListener(
        		new RadioGroup.OnCheckedChangeListener() {
        		public void onCheckedChanged(
        		RadioGroup group,
        		int checkedId) {
						theme = checkedId-R.id.set_preference_radioButtonTheme0;
						System.out.println("select theme: "+checkedId);
        		}
        		});
	}
	
	public void setFontSize(int sizeNum){
		this.fontSizeResult.setTextSize(Integer.parseInt(fontSizes[sizeNum]));
		this.fontSize = Integer.parseInt(fontSizes[sizeNum]);
	}
	
	public void setFontColor(int color){
		this.fontSizeResult.setTextColor(color);
	}
	
	public int getFontSize(){
		//return (int)this.fontSizeResult.getTextSize();
		return this.fontSize;
	}
	
	public int getFontColor(){
		return fontColor;
	}
	
	public void setEncryAlgorithm(int num){
		this.encryAlgorithm = num;
	}
	
	public int getEncryAlgorithm(){
		return this.encryAlgorithm;
	}
	
	public static SetPreferenceActivity getInstance(){
		return instance;
	}
}
